<?php

namespace Lib;

use Lib\Util\Util,
    Lib\Exception\NotFoundException;

class Request {

    public static function post($name = null) {
        $data  = isset($_POST) ? $_POST : false;
        $value = isset($name) ? @$data[$name] : $data;
        return $value ? (object)$value : false;
    }

    public static function get($name = null) {
        $data  = isset($_GET) ? $_GET : false;
        $value = isset($name) ? @$data[$name] : $data;
        return $value ? (object)$value : false;
    }

    public static function file($name = null) {
        $data  = isset($_FILES) ? $_FILES : false;
        $value = isset($name) ? @$data[$name] : $data;
        return $value ? $value : false;
    }

    public static function referer($default = '/') {
        return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $default;
    }

    public static function isActive($url) {
        $urlServer = urldecode($_SERVER['REQUEST_URI']);
        try {
            $urlRequest    = Dispatcher::parseRequest($url);
            $serverRequest = Dispatcher::parseRequest($urlServer);

            return $urlRequest == $serverRequest;

        } catch(NotFoundException $e) {
            return $url == $urlServer;
        }
    }

    public static function server() {
        $name = $_SERVER['SERVER_NAME'];
        $name = preg_replace('/^www\./', '', $name);
        return $name;
    }

}

?>